<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
require ('./order.php');

require ('./Sphp.php');
session_start();  
//echo $_SESSION['Role'];

$Role = $_SESSION['Role'];

$sqlquery="select T0.[RelatedTable],T0.[RelatedColumn],T1.Description from [dbo].[AX_Replace_Role_Right] T0 left join [dbo].[AX_Order_Column_Display] T1 ON T0.[RelatedTable]=T1.[RelatedTable] AND
T0.[RelatedColumn]=T1.[RelatedColumn] WHERE T0.[role]='".$Role."' and T0.IsVisable='Y' order by visorder";
$datelist=array();
$title="";
//echo $sqlquery;
$arr=odbc_exec($sqlconnection,$sqlquery);
$sqlmid='';
$sqlleft='Select 0';
// 使用while语句循环mysql_fetch_array()函数返回的数组
while($result=odbc_fetch_array($arr)){
$sqlmid=$sqlmid.','.$result['RelatedColumn'];
$title=$title."
<th>".iconv("GB18030//IGNORE","utf-8",$result['Description'])."</th>";
$colid[]=$result['RelatedColumn'];
}

$sqlright=",[dbo].[AX_lastvandor](U_ItemCode) AX_lastvandor  from AX_replace_order WHERE U_OStatus='2' and ( U_PManager = '".$_SESSION['Pleader']."' )";
$ownsql=$sqlleft.$sqlmid.$sqlright;
//ECHO $ownsql;
$arr2=odbc_do($sqlconnection,$ownsql);
$mx=count($colid);

$time = time();
$name="execltemplate".$time.".xlsx";
$outputFileName = $name; 
//copy("execltemplate/phpexecltemplate.xlsx", "temp/$outputFileName");

require_once 'Classes/PHPExcel/IOFactory.php';
while($result2=odbc_fetch_array($arr2)){
$items[]=$result2;
} 

/** PHPExcel */require_once 'Classes/PHPExcel.php';

$excel2 = PHPExcel_IOFactory::createReader('Excel2007');
$excel2 = $excel2->load('execltemplate/phpexecltemplate.xlsx');
$excel2->setActiveSheetIndex(0);
$i = 3; 

foreach($items as $item){
$excel2->getActiveSheet()->setCellValueExplicit('A' . $i, $item['U_OrderId'], PHPExcel_Cell_DataType::TYPE_STRING);
$excel2->getActiveSheet()->setCellValue('B' . $i, $item['DocEntry']); 
$excel2->getActiveSheet()->setCellValue('C' . $i, iconv("GB18030//IGNORE","utf-8",$item['U_ItemCode'])); 
$excel2->getActiveSheet()->setCellValue('D' . $i, iconv("GB18030//IGNORE","utf-8",$item['U_PManager'])); 
$excel2->getActiveSheet()->setCellValue('E' . $i, $item['U_CitemCd']); 
$excel2->getActiveSheet()->setCellValue('F' . $i, iconv("GB18030//IGNORE","utf-8",$item['U_CitemNm'])); 
$excel2->getActiveSheet()->setCellValue('G' . $i, $item['U_Qty']); 
$excel2->getActiveSheet()->setCellValue('H' . $i, iconv("GB18030//IGNORE","utf-8",$item['U_State'])); 
$excel2->getActiveSheet()->setCellValue('I' . $i, iconv("GB18030//IGNORE","utf-8",$item['U_City'])); 
$excel2->getActiveSheet()->setCellValue('J' . $i, iconv("GB18030//IGNORE","utf-8",$item['U_Address'])); 
$excel2->getActiveSheet()->setCellValue('K' . $i, iconv("GB18030//IGNORE","utf-8",$item['U_Cstname'])); 
$excel2->getActiveSheet()->setCellValue('L' . $i, $item['U_Phone']); 
$excel2->getActiveSheet()->setCellValue('Q' . $i, iconv("GB18030//IGNORE","utf-8",$item['AX_lastvandor'])); 
$i ++; 
}

$objWriter = PHPExcel_IOFactory::createWriter($excel2, 'Excel2007');
$objWriter->save('temp/'.$outputFileName);

echo '<a href="temp/'.$outputFileName.'">下载</a>';
//echo '<a href="./temp/'.$outputFileName.'">下载</a>';


?>
